using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class CashTodayDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CashTodayDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CashToday table.
		/// </summary>
		public virtual void Insert(CashTodayInfo cashTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", cashTodayInfo.IdCash),
				new SqlParameter("@PostDate", cashTodayInfo.PostDate),
				new SqlParameter("@IdCommon", cashTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", cashTodayInfo.AccountNo),
				new SqlParameter("@Cash", cashTodayInfo.Cash),
				new SqlParameter("@SecType", cashTodayInfo.SecType)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CashToday_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CashToday table.
		/// </summary>
		public virtual void Update(CashTodayInfo cashTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", cashTodayInfo.IdCash),
				new SqlParameter("@PostDate", cashTodayInfo.PostDate),
				new SqlParameter("@IdCommon", cashTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", cashTodayInfo.AccountNo),
				new SqlParameter("@Cash", cashTodayInfo.Cash),
				new SqlParameter("@SecType", cashTodayInfo.SecType)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CashToday_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CashToday table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idCash)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", idCash)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CashToday_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CashToday table.
		/// </summary>
		public virtual CashTodayInfo Select(Guid? idCash)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", idCash)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CashToday_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCashTodayInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CashToday table.
		/// </summary>
		public List<CashTodayInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CashToday_SelectAll"))
			{
				List<CashTodayInfo> cashTodayInfoList = new List<CashTodayInfo>();
				while (dataReader.Read())
				{
					cashTodayInfoList.Add(MakeCashTodayInfo(dataReader));
				}

				return cashTodayInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CashToday class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CashTodayInfo MakeCashTodayInfo(SqlDataReader dataReader)
		{
			CashTodayInfo cashTodayInfo = new CashTodayInfo();
			cashTodayInfo.IdCash = SqlClientUtility.GetGuid(dataReader, DbConstants.CASHTODAY.IDCASH);
			cashTodayInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.CASHTODAY.POSTDATE);
			cashTodayInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.CASHTODAY.IDCOMMON);
			cashTodayInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.CASHTODAY.ACCOUNTNO);
			cashTodayInfo.Cash = SqlClientUtility.GetDecimal(dataReader, DbConstants.CASHTODAY.CASH);
			cashTodayInfo.SecType = SqlClientUtility.GetString(dataReader, DbConstants.CASHTODAY.SECTYPE);

			return cashTodayInfo;
		}

		#endregion
	}
}
